﻿using System;

using Common;

namespace TortoisePlugin.Settings
{
    public class SettingsManager : Common.SettingsManager
    {
        public enum eTORTOISE
        {
            eTORTOISE_NOT_SET,
            eTORTOISE_SVN_CVS_HG,   // Repository revision is a number
            eTORTOISE_GIT,          // Repository revision is an hexadecimal value
            eTORTOISE_NB_VALUE      // Keep last
        }

        public String[] sTORTOISE = { "NOT_SET", "CVS", "GIT" };

        public SettingsManager()
            : base()
        {
            Add("TORTOISE");
        }
        
        public SettingsManager(String parameters)
            : this()
        {
            Set(parameters);
        }

        public override string this[string key]
        {
            get
            {
                switch (key.ToUpperInvariant())
                {
                    case "TORTOISE":
                        return iTortoise.ToString();
                }

                return base[key];
            }
            set
            {
                switch (key.ToUpperInvariant())
                {
                    case "TORTOISE":
                        {
                            int newValue;
                            if (int.TryParse(value, out newValue))
                                iTortoise = newValue;
                            break;
                        }
                    default:
                        base[key] = value;
                        break;
                }

            }
        }

        public String Tortoise
        {
            get { return base["TORTOISE"]; }
            set { base["TORTOISE"] = value.ToUpperInvariant(); }
        }

        public eTORTOISE eTortoise
        {
            get
            {
                String tortoise = Tortoise.ToUpperInvariant();

                for (eTORTOISE e = 0; e < eTORTOISE.eTORTOISE_NB_VALUE; ++e)
                {
                    if (tortoise.Equals(sTORTOISE[(int)e].ToUpperInvariant()))
                        return e;
                }

                return eTORTOISE.eTORTOISE_NOT_SET;
            }

            set
            {
                Tortoise = sTORTOISE[(int)value];
            }
        }

        public int iTortoise
        {
            get { return (int)eTortoise; }
            set { eTortoise = (eTORTOISE)value; }
        }

        public override bool ValidateParameter(Common.Controls.SettingsControl sc)
        {
            (sc as SettingsControl).ResetTest();

            if (eTortoise != eTORTOISE.eTORTOISE_NOT_SET)
            {
                (sc as SettingsControl).Success(0);
                return true;
            }
            else
            {
                (sc as SettingsControl).Fail(0);
                return false;
            }
        }
    }
}